﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using H2.NET.Web.Workflow.Services;
using H2.NET.Web.Workflow.DwzCode;
using System.Web.UI.WebControls;
using H2.NET.Common;
using System.Data;
using H2.NET.Web.Management.Config.Service;

namespace H2.NET.Web.Management.Config
{
    public partial class Index : BasePage
    {
        private readonly ConfigService _configService = new ConfigService();
        public string name;
        public String parentId;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            string _action = AksRequest.GetQueryString("action");

            if (_action.Equals("delete"))
            {
                int id = AksRequest.GetFormInt("id");
                //_configService.Delete(id);
                _configService.deleteAllChild(id);
                
                SuccessMsg("configIndex");
                return;
            }
            else if (_action.Equals("menu"))
            {
                string html = "";
                DataTable menu = new DataTable();
                menu.Columns.Add("id");
                menu.Columns.Add("parent_id");
                menu.Columns.Add("name");
                menu.Columns.Add("url");
                menu.Columns.Add("layer");
                menu.Columns.Add("RelCode");
                DataTable ds = _configService.GetListByPage("ParentId IS NULL", "seq",
                                                              PageSize * (PageNumber) + 1,
                                                              PageSize * (PageNumber + 1)).Tables[0];
                for (int i = 0; i < ds.Rows.Count; i++)
                {
                    if (ds.Rows[i]["code"].ToString().Contains("M_"))
                    {
                        DataRow dr = menu.NewRow();
                        dr["id"] = 33;
                        dr["parent_id"] = 44;
                        dr["name"] = ds.Rows[i]["name"].ToString();
                        dr["url"] = "/Management/Config/IndexMany.aspx?pId=" + ds.Rows[i]["configid"].ToString();
                        dr["layer"] = 2;
                        dr["RelCode"] = "ConfigIndex_" + ds.Rows[i]["configid"].ToString();
                        menu.Rows.Add(dr);
                    }
                    else
                    {
                        DataRow dr = menu.NewRow();
                        dr["id"] = 33;
                        dr["parent_id"] = 44;
                        dr["name"] = ds.Rows[i]["name"].ToString();
                        dr["url"] = "/Management/Config/Index.aspx?pId=" + ds.Rows[i]["configid"].ToString() + "&mid=33";
                        dr["layer"] = 2;
                        dr["RelCode"] = "ConfigIndex_" + ds.Rows[i]["configid"].ToString();
                        menu.Rows.Add(dr);
                    }
                }

                    for (int j = 0; j < menu.Rows.Count-1; j++)
                    {
                        html += "<li><div class><div class='node'></div><div class='file'></div><a href=" + menu.Rows[j]["url"].ToString() + " target='navTab' rel=" + menu.Rows[j]["relcode"].ToString() + ">" + menu.Rows[j]["name"].ToString() + "</a></div></li>";
                    }
                    html += "<li class='last'><div class><div class='node'></div><div class='file'></div><a href=" + menu.Rows[menu.Rows.Count - 1]["url"].ToString() + " target='navTab' rel=" + menu.Rows[menu.Rows.Count - 1]["relcode"].ToString() + ">" + menu.Rows[menu.Rows.Count - 1]["name"].ToString() + "</a></div></li>";
                

                    Response.Clear();
                    Response.ContentType = "text/plain";
                    Response.Write(html);
                    Response.End();
               
            }
            
            String pId = AksRequest.GetQueryString("pId");
            rel.Value = pId; 
            hd_parentId.Value = pId;
            string strWhere = "1=1";

            if (!string.IsNullOrEmpty(Request.Form["name"]))
            {
                strWhere += " and Name like '%" + Request.Form["name"].Trim() + "%'";
            }

            parentId = AksRequest.GetFormString("parentId");
            if (pId != null && !pId.Equals(""))
            {
                strWhere += " AND ParentId =" + pId + " ";
            }
            else 
            {
                strWhere += " and ParentId IS NULL ";
            }

            
            TotalItemCount = _configService.GetRecordCount(strWhere);
            PageNumber = Convert.ToInt32(Request.Form["pageNum"] ?? "1");
            Pager1.TotalItemCount = TotalItemCount;
            Pager1.PageSize = PageSize;
            Pager1.PageNumber = PageNumber;
            

            rpt_configs.DataSource = _configService.GetListByPage(strWhere, "Seq",
                                                              PageSize * (PageNumber-1) + 1,
                                                              PageSize * PageNumber);
            rpt_configs.DataBind();

            //SuccessMsg("sjzd");
            name = Request.Form["name"];
 
        }
    }
}